<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Login.php
 *
 * @author  :  Skiychan <developer@zzzzy.com>
 * @created :  2015/5/28
 * @modified:  
 * @version :  0.0.1
 */

class Login {
    protected $ci;
    protected $check_controller = array(
        "v1"=>array("user_profile","user_files"),
        "mobile"=>array("user_profile","user_files"),
        "pc"=>array("user_files"),
        "web"=>array("user_profile","files"),
    );
    protected $check_white_list = array("");

    public function __construct() {
        $this->ci = &get_instance();
        if(!$this->ci)RETURN TRUE;
    }

    public function login_check() {
        if(!$this->ci)RETURN TRUE;
        $method = strtolower($this->ci->input->server('REQUEST_METHOD'));
        if($method=="options"){
            Common::ajax_return(0);
        }
        //$uri = $this->get_url();
        $dir = str_replace("/","",$this->ci->router->directory);
        if(!empty($dir) && isset($this->check_controller[$dir]) && in_array($this->ci->router->class, $this->check_controller[$dir])) {
            return $this->valid();
        }
        return TRUE;
    }

    /**
     * 验证登陆
     * @return array|bool
     */
    private function valid() {
        $this->ci->load->service('login_service');
        $user_info = $this->ci->login_service->is_login();
        if ($user_info === FALSE) {
            return Common::ajax_return(1011);
        } else {
            return TRUE;
        }
    }

    /**
     * 取得控制器和方法
     * @return string
     */
    private function get_url() {
        $controller = $this->ci->router->class;
        $action = $this->ci->router->method;
        return $controller."/".$action;
    }
}